package com.customview.CustomLineCoordinates;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

/**
 * @author Sandsboy
 * @description:广州皓醒科技有限公司
 * @date :2019/11/6 14:33
 * 单独坐标自定义
 */
public class CustomLineCoordinates extends View {
    public CustomLineCoordinates(Context context) {
        this(context, null);
    }

    public CustomLineCoordinates(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);

    }

    public CustomLineCoordinates(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    Paint mPaint;
    Paint mTextPaint;
    Paint mUnitPaint;

    private void init() {
        mPaint = new Paint();
        mPaint.setStyle(Paint.Style.STROKE);
        mTextPaint = new Paint();
        mUnitPaint = new Paint();


    }

    /**
     * 获取随机字样
     *
     * @return
     */
    public String getRandomText() {

        double random = Math.random();
        int randint = (int) (random * 10);
        return random + "";
    }


    int Xnum = 10;
    int Ynum = 10;
    int BasePadding = 20;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //xy两条线条
        drawXYLine(canvas);
        drawXValues(canvas);
        drawYValues(canvas);
        drawYUnit(canvas);
        drawXUnit(canvas);
    }

    private void drawXUnit(Canvas canvas) {

        //最高值
        int maxUnit = 100;
        int FlagUint = 5;
        //计算总高度
        int totoal = getWidth() - BasePadding * 2;
        //标尺长度
        int rulerWidth=10;
        int avUint=totoal/ maxUnit;
        Paint.FontMetrics p=mTextPaint.getFontMetrics();
        float TextBottom=p.bottom;

        for (int i = 0; i <maxUnit+1; i++) {
            if (i==0){

            }else if (i%5==0){
                canvas.drawLine(BasePadding+avUint*i,getHeight()-BasePadding,BasePadding+avUint*i,getHeight()-rulerWidth-BasePadding,mUnitPaint);

               float textWidth =mTextPaint.measureText(i+"");
                canvas.drawText(i+"",BasePadding+avUint*i-textWidth/2,getHeight()-BasePadding-rulerWidth-TextBottom,mTextPaint);

            }else {

            }
        }
    }

    private void drawYUnit(Canvas canvas) {


        //最高值
        int maxUnit = 100;
        int FlagUint = 5;
        //计算总高度
        int totoal = getHeight() - BasePadding * 2;
        //标尺长度
        int rulerWidth=10;
        int avUint=totoal/ maxUnit;
        Paint.FontMetrics p=mTextPaint.getFontMetrics();
        float TextBottom=p.bottom;

        for (int i = 0; i <maxUnit+1; i++) {
            if (i==0){
                canvas.drawLine(BasePadding,getHeight()-avUint*i-BasePadding,BasePadding+rulerWidth,getHeight()-avUint*i-BasePadding,mUnitPaint);
               canvas.drawText(i+"",BasePadding+rulerWidth,getHeight()-avUint*i-BasePadding+TextBottom,mTextPaint);
            }else if (i%5==0){
                canvas.drawLine(BasePadding,getHeight()-avUint*i-BasePadding,BasePadding+rulerWidth,getHeight()-avUint*i-BasePadding,mUnitPaint);
                canvas.drawText(i+"",BasePadding+rulerWidth,getHeight()-avUint*i-BasePadding+TextBottom,mTextPaint);

            }else {

            }
        }

    }

    private void drawYValues(Canvas canvas) {

    }

    private void drawXValues(Canvas canvas) {

        canvas.drawLine(BasePadding, getHeight() - BasePadding, BasePadding, BasePadding, mPaint);
    }

    private void drawXYLine(Canvas canvas) {
        canvas.drawLine(BasePadding, getHeight() - BasePadding, getWidth() - BasePadding, getHeight() - BasePadding, mPaint);

    }

    int DefaultHeight = 400;
    int DefaultWidth = 400;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int sizewidth = MeasureSpec.getSize(widthMeasureSpec);
        int modewidth = MeasureSpec.getMode(widthMeasureSpec);

        int sizeheight = MeasureSpec.getSize(heightMeasureSpec);
        int modeheight = MeasureSpec.getMode(heightMeasureSpec);

        switch (modewidth) {
            case MeasureSpec.AT_MOST://wrap
                DefaultWidth = DefaultHeight;
                break;
            case MeasureSpec.EXACTLY://match 或者精确
                DefaultWidth = sizewidth;
                break;

        }


        switch (modeheight) {
            case MeasureSpec.AT_MOST://wrap
                DefaultHeight = DefaultHeight;
                break;
            case MeasureSpec.EXACTLY://match 或者精确
                DefaultHeight = sizeheight;
                break;

        }
        setMeasuredDimension(DefaultWidth, DefaultHeight);
    }
}
